﻿@page
@using SoEasyPlatform
@model SoEasyPlatform.Pages.TagPropertyInfoModel
@{
    var tableId = Convert.ToInt32(Request.Query["tableid"] + "");
    var tableInfo = new Repository<CodeTable>().GetById(tableId);
    List<CodeColumns> columns = new Repository<CodeColumns>().GetList(it => it.CodeTableId == tableId);
    List<TagProperty> tagpros = new Repository<TagProperty>().GetList();
    var mappings = new Repository<MappingProperty>().GetList(m=>m.TableName==tableInfo.TableName);    
    Layout = "~/Pages/Shared/_Window.cshtml";
}
<script>
    var tableName = "@tableInfo.ClassName";
    /**
     * 原结构：columns:[{key:ClassProperName,value：TagId}]
     * 修改后数据结构：
     * {
     *  table: tableName, 
     *   columns: [
     *     "ClassProperName":[{"tagId":tagId},{"tagValue":tageValue}]
     *   ]
     * }
     * 
     */
    function GetData()
    {
        var result = [];
        $("#tbbox .tr").each(function () {
            var th = $(this);
            var values = [];
            th.find(":checked").each(function () {
                var item = $(this);              
                values.push({ TagId: item.val(),TagValue:"1" })
            });
            th.find(".tag-text-input").each(function () {
                var item = $(this);
                var value = item.val();
                if (value != undefined && value!=null&&value.length>0)
                { 
                    values.push({ TagId: item.attr("name"), TagValue: item.val() });
                }                
             });
            result.push({ "ClassProperName": th.find("[name=ClassProperName]").val(), Tags: values });
        });
        //原结构：columns:[{key:ClassProperName,value：TagId}]
        console.log(`result is ${JSON.stringify(result)}`);
        return { table: tableName, columns: result };
    }
</script>
<style>
    td ,th{
        padding: 10px;
        font-size: 14px;
        width: 20%;        
    }
    tr{
        border:solid 1px;
    }
    tr:hover {
        background: #ccc;
    }
    th:hover {
        background: #fff;
    }
</style>
<div style="height:100%;overflow-y:scroll;padding-left:8px">
    <table id="tbbox" style="width:100%">
        <tr>
            <th colspan="@(tagpros.Count+1)">    @(tagpros.Count==0?"对不起没你还没有配置扩展属性，请到【扩展属性】菜单配置" : @tableInfo.TableName) </th>
        </tr>
        @foreach (var col in columns)
        {
            <tr class="tr">
                <td>
                    @col.ClassProperName @col.Description
                    <input type="hidden" name="ClassProperName" value="@col.ClassProperName" />
                </td>
                @foreach (TagProperty tgp in tagpros)
                {
                    <td style="width:20%">
                        @{
                            var inputType = tgp.ControlType?.ToLower();
                            var tagMap = mappings.Where(tm => tm.ColumnName == col.ClassProperName && tm.TagId == tgp.Id.ToString())?.FirstOrDefault();
                            var tagValue = tagMap == null ? tgp.FieldValue : tagMap.TagValue;
                            switch (inputType)
                            {
                                case "text":
                                case "textbox":
                                    @: &nbsp; @tgp.FieldName: <input type="text" class="tag-text-input" value="@tagValue" name="@tgp.Id" id="@tgp.Id" />
                                    break;
                                default:
                                    if (!string.IsNullOrEmpty(tagValue) && tagValue != "0" && tagValue.ToLower() != "false")
                                    {
                                        @: &nbsp; <input type="checkbox" checked="checked" value="@tgp.Id" /> @tgp.FieldName
                                    }
                                    else
                                    {
                                        @: &nbsp; <input type="checkbox" value="@tgp.Id" /> @tgp.FieldName
                                    }
                                    break;
                            }
                        }
                    </td>
                }
            </tr>
        }
    </table>
</div>